
package gamepad.vistas;

import gamepad.GamePad;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultBoundedRangeModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class VistaGamePad extends javax.swing.JDialog {

	private static final long serialVersionUID = 1L;
	
	private GamePad gamepad;

    public VistaGamePad(java.awt.Frame parent, boolean modal, GamePad g) {
        super(parent, modal);
        gamepad = g;
        
        initComponents();
        configuracionInicial();
    }

    private void configuracionInicial() {
		actualizacionSpinner.setValue(gamepad.getUpdateInterval());
		actualizacionSlider.setValue(gamepad.getUpdateInterval());
		deadzoneXSpinner.setValue(new Double(gamepad.getDeadzoneX()));
		deadzoneXSlider.setValue(((Float)(gamepad.getDeadzoneX() * 100)).intValue());
		deadzoneYSpinner.setValue(new Double(gamepad.getDeadzoneY()));
		deadzoneYSlider.setValue(((Float)(gamepad.getDeadzoneY() * 100)).intValue());
		limiteXSpinner.setValue(new Double(gamepad.getLimiteX()));
		limiteXSlider.setValue(((Float)(gamepad.getLimiteX() * 10)).intValue());
		limiteYSpinner.setValue(new Double(gamepad.getLimiteY()));
		limiteYSlider.setValue(((Float)(gamepad.getLimiteY() * 10)).intValue());
		anguloLimiteSpinner.setValue(gamepad.getAnguloLimite());
		anguloLimiteSlider.setValue(gamepad.getAnguloLimite());
	}
    
    private void configuracionPorDefecto() {
    	actualizacionSpinner.setValue(GamePad.UPDATE_INTERVAL);
		actualizacionSlider.setValue(GamePad.UPDATE_INTERVAL);
		deadzoneXSpinner.setValue(new Double(GamePad.DEADZONE_X));
		deadzoneXSlider.setValue(((Float)(GamePad.DEADZONE_X * 100)).intValue());
		deadzoneYSpinner.setValue(new Double(GamePad.DEADZONE_Y));
		deadzoneYSlider.setValue(((Float)(GamePad.DEADZONE_Y * 100)).intValue());
		limiteXSpinner.setValue(new Double(GamePad.LIMITE_X));
		limiteXSlider.setValue(((Float)(GamePad.LIMITE_X * 10)).intValue());
		limiteYSpinner.setValue(new Double(GamePad.LIMITE_Y));
		limiteYSlider.setValue(((Float)(GamePad.LIMITE_Y * 10)).intValue());
		anguloLimiteSpinner.setValue(GamePad.ANGULO_LIMITE);
		anguloLimiteSlider.setValue(GamePad.ANGULO_LIMITE);
    }

	/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        deadzoneYSpinner = new javax.swing.JSpinner();
        actualizacionLabel = new javax.swing.JLabel();
        deadzoneXSpinner = new javax.swing.JSpinner();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        gamepadPanel = new javax.swing.JPanel();
        limiteXLabel = new javax.swing.JLabel();
        limiteYLabel = new javax.swing.JLabel();
        limiteXSlider = new javax.swing.JSlider();
        limiteXSpinner = new javax.swing.JSpinner();
        limiteYSlider = new javax.swing.JSlider();
        limiteYSpinner = new javax.swing.JSpinner();
        volantePanel = new javax.swing.JPanel();
        anguloLimiteLabel = new javax.swing.JLabel();
        anguloLimiteSlider = new javax.swing.JSlider();
        anguloLimiteSpinner = new javax.swing.JSpinner();
        deadzoneYSlider = new javax.swing.JSlider();
        deadzoneYLabel = new javax.swing.JLabel();
        deadzoneXLabel = new javax.swing.JLabel();
        actualizacionSlider = new javax.swing.JSlider();
        actualizacionSpinner = new javax.swing.JSpinner();
        deadzoneXSlider = new javax.swing.JSlider();
        okButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        cancelarButton = new javax.swing.JButton();
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        
        actualizacionLabel.setText("Intervalo de Actualización:");
        
        limiteXLabel.setText("Límite (eje X):");
       
        limiteYLabel.setText("Límite (eje Y):");
        
        actualizacionSpinner.setModel(new SpinnerNumberModel(25, 0, 300, 5));
        deadzoneXSpinner.setModel(new SpinnerNumberModel(0.3, 0.0, 2.0, 0.05));
        deadzoneYSpinner.setModel(new SpinnerNumberModel(0.25, 0.0, 2.0, 0.05));
        limiteXSpinner.setModel(new SpinnerNumberModel(6.0, 1.0, 10.0, 0.5));
        limiteYSpinner.setModel(new SpinnerNumberModel(5.0, 1.0, 10.0, 0.5));
        anguloLimiteSpinner.setModel(new SpinnerNumberModel(160, 20, 180, 5));
        
        actualizacionSlider.setModel(new DefaultBoundedRangeModel(25, 0, 0, 300));
        deadzoneXSlider.setModel(new DefaultBoundedRangeModel(30, 0, 0, 200));
        deadzoneYSlider.setModel(new DefaultBoundedRangeModel(25, 0, 0, 200));
        limiteXSlider.setModel(new DefaultBoundedRangeModel(60, 0, 10, 100));
        limiteYSlider.setModel(new DefaultBoundedRangeModel(50, 0, 10, 100));
        anguloLimiteSlider.setModel(new DefaultBoundedRangeModel(160, 0, 20, 180));
        
        actualizacionSlider.setMinorTickSpacing(5);
        actualizacionSlider.setSnapToTicks(true);
        deadzoneXSlider.setMinorTickSpacing(5);
        deadzoneXSlider.setSnapToTicks(true);
        deadzoneYSlider.setMinorTickSpacing(5);
        deadzoneYSlider.setSnapToTicks(true);
        limiteXSlider.setMinorTickSpacing(5);
        limiteXSlider.setSnapToTicks(true);
        limiteYSlider.setMinorTickSpacing(5);
        limiteYSlider.setSnapToTicks(true);
        anguloLimiteSlider.setMinorTickSpacing(5);
        anguloLimiteSlider.setSnapToTicks(true);
        		
        actualizacionSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				actualizacionSlider.setValue((Integer)actualizacionSpinner.getValue());
			}
		});
        deadzoneXSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				deadzoneXSlider.setValue(((Double)(((Double)deadzoneXSpinner.getValue()) * 100)).intValue());
			}
		});
        deadzoneYSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				deadzoneYSlider.setValue(((Double)(((Double)deadzoneYSpinner.getValue()) * 100)).intValue());
			}
		});
        limiteXSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				limiteXSlider.setValue(((Double)(((Double)limiteXSpinner.getValue()) * 10)).intValue());
			}
		});
        limiteYSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				limiteYSlider.setValue(((Double)(((Double)limiteYSpinner.getValue()) * 10)).intValue());
			}
		});
        anguloLimiteSpinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				anguloLimiteSlider.setValue((Integer)anguloLimiteSpinner.getValue());
			}
		});
        
        actualizacionSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				actualizacionSpinner.setValue(actualizacionSlider.getValue());
			}
		});
        deadzoneXSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				deadzoneXSpinner.setValue(new Double(deadzoneXSlider.getValue()) / 100);
			}
		});
        deadzoneYSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				deadzoneYSpinner.setValue(new Double(deadzoneYSlider.getValue()) / 100);
			}
		});
        limiteXSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				limiteXSpinner.setValue(new Double(limiteXSlider.getValue()) / 10);
			}
		});
        limiteYSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				limiteYSpinner.setValue(new Double(limiteYSlider.getValue()) / 10);
			}
		});
        anguloLimiteSlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				anguloLimiteSpinner.setValue(anguloLimiteSlider.getValue());
			}
		});
        
        javax.swing.GroupLayout gamepadPanelLayout = new javax.swing.GroupLayout(gamepadPanel);
        gamepadPanel.setLayout(gamepadPanelLayout);
        gamepadPanelLayout.setHorizontalGroup(
            gamepadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gamepadPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(gamepadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(gamepadPanelLayout.createSequentialGroup()
                        .addComponent(limiteXLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(limiteXSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(gamepadPanelLayout.createSequentialGroup()
                        .addComponent(limiteYLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(limiteYSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(gamepadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(limiteYSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(limiteXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2487, 2487, 2487))
        );
        gamepadPanelLayout.setVerticalGroup(
            gamepadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gamepadPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(gamepadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(limiteXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(limiteXSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(limiteXLabel))
                .addGap(18, 18, 18)
                .addGroup(gamepadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(limiteYSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(limiteYLabel)
                    .addComponent(limiteYSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Gamepad", gamepadPanel);

        anguloLimiteLabel.setText("Ángulo Límite:");

        javax.swing.GroupLayout volantePanelLayout = new javax.swing.GroupLayout(volantePanel);
        volantePanel.setLayout(volantePanelLayout);
        volantePanelLayout.setHorizontalGroup(
            volantePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(volantePanelLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(anguloLimiteLabel)
                .addGap(3, 3, 3)
                .addComponent(anguloLimiteSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(anguloLimiteSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        volantePanelLayout.setVerticalGroup(
            volantePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(volantePanelLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(volantePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(anguloLimiteSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(anguloLimiteLabel)
                    .addComponent(anguloLimiteSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jTabbedPane1.addTab("Volante", volantePanel);

        deadzoneYLabel.setText("Zona Muerta (eje Y):");

        deadzoneXLabel.setText("Zona Muerta (eje X):");

        okButton.setText("OK");
        okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				okButtonActionPerformed(e);
			}
		});
        
        resetButton.setText("Valores por Defecto");
        resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetButtonActionPerformed(e);
			}
		});
        
        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cancelarButtonActionPerformed(e);
			}
		});
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.CENTER, layout.createSequentialGroup()
                        .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                        .addGap(32, 32, 32)
                        .addComponent(resetButton, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                        .addGap(34, 34, 34)
                        .addComponent(cancelarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.CENTER, 0, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(actualizacionLabel)
                            .addComponent(deadzoneXLabel)
                            .addComponent(deadzoneYLabel))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(actualizacionSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(deadzoneXSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(deadzoneYSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(actualizacionSpinner)
                            .addComponent(deadzoneYSpinner)
                            .addComponent(deadzoneXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(actualizacionLabel)
                    .addComponent(actualizacionSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(actualizacionSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(deadzoneXLabel)
                    .addComponent(deadzoneXSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deadzoneXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(deadzoneYLabel)
                    .addComponent(deadzoneYSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deadzoneYSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cancelarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                        .addComponent(resetButton, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    protected void okButtonActionPerformed(ActionEvent e) {
		gamepad.setUpdateInterval((Integer)actualizacionSpinner.getValue());
		gamepad.setDeadzones(((Double)deadzoneXSpinner.getValue()).floatValue(), ((Double)deadzoneYSpinner.getValue()).floatValue());
		gamepad.setLimites(((Double)limiteXSpinner.getValue()).floatValue(), ((Double)limiteYSpinner.getValue()).floatValue());
		gamepad.setAnguloLimite((Integer)anguloLimiteSpinner.getValue());
		dispose();
	}
    
    protected void resetButtonActionPerformed(ActionEvent e) {
		configuracionPorDefecto();
	}
	
	protected void cancelarButtonActionPerformed(ActionEvent e) {
		dispose();
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel actualizacionLabel;
    private javax.swing.JSlider actualizacionSlider;
    private javax.swing.JSpinner actualizacionSpinner;
    private javax.swing.JLabel anguloLimiteLabel;
    private javax.swing.JSlider anguloLimiteSlider;
    private javax.swing.JSpinner anguloLimiteSpinner;
    private javax.swing.JButton cancelarButton;
    private javax.swing.JLabel deadzoneXLabel;
    private javax.swing.JSlider deadzoneXSlider;
    private javax.swing.JSpinner deadzoneXSpinner;
    private javax.swing.JLabel deadzoneYLabel;
    private javax.swing.JSlider deadzoneYSlider;
    private javax.swing.JSpinner deadzoneYSpinner;
    private javax.swing.JPanel gamepadPanel;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel limiteXLabel;
    private javax.swing.JSlider limiteXSlider;
    private javax.swing.JSpinner limiteXSpinner;
    private javax.swing.JLabel limiteYLabel;
    private javax.swing.JSlider limiteYSlider;
    private javax.swing.JSpinner limiteYSpinner;
    private javax.swing.JButton okButton;
    private javax.swing.JButton resetButton;
    private javax.swing.JPanel volantePanel;
    // End of variables declaration//GEN-END:variables

}
